#include<string>
#include<vector>
#include<unordered_map>
using namespace std;

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int>map;
        int cnt = 0;
        int left = 0, right = 0;
        while (right < s.length()) {
            char idx = s[right++];
            ++map[idx];
            if (map[idx] > 1)
                ++cnt;
            if (cnt != 0) {
                idx = s[left++];
                --map[idx];
                if (map[idx] > 0)
                    --cnt;
            }
        }
        return right - left;
    }
};